﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using csIOC0640;
using Common;

namespace LCRReader
{
    public partial class usrSet : Form
    {
        Comm _comm = Comm.Inst;
        I_IO_Card _io = IO_C0640.instance;

        portconfigAccess db = new portconfigAccess();
        GenneralConfigAccess config = GenneralConfigAccess.Inst;
        public usrSet()
        {
            InitializeComponent();
            comboBox1.Items.Clear();
            comboBox1.Items.AddRange(Comm.getAvaliComms());
            this.pictureBox1.BackColor = _comm.isOpen ? Color.LimeGreen: Color.Gray;
            this.textBox1.Text = GenneralConfigAccess.Inst.GetValue("SAVE_PATH");
            var portconfig = db.GetportConfigByName();
            this.comboBox1.Text = portconfig.COM;
            this.comboBox2.Text = portconfig.Baudrate.ToString();
            this.comboBox3.Text = portconfig.DataBits.ToString();
            this.comboBox4.Text = portconfig.Stopbits;
            this.comboBox5.Text = portconfig.Parity;
            this.checkBox1.Checked = config.GetValue("ENABLE_COMM").ToUpper()=="TRUE"?true:false;
            this.checkBox2.Checked = (config.GetValue("ENABLE_RESULT")).ToUpper() == "TRUE" ? true : false;
            string value;
            value = config.GetValue("DAY");
            try
            {
                dateTimePicker1.Value = DateTime.Parse(value.Split(',')[0]);
                dateTimePicker2.Value = DateTime.Parse(value.Split(',')[1]);
            }
            catch (Exception)
            {
                
             //   throw;
            }
            this.textBox2.Text = config.GetValue("LINE");
            
          //  config.SetValue("LINE", this.textBox2.Text);

        //    IOC0640.ioc_board_init();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            _comm.Open(this.comboBox1.Text, int.Parse(this.comboBox2.Text), int.Parse(this.comboBox3.Text), comboBox4.Text, comboBox5.Text);
            this.pictureBox1.BackColor = _comm.isOpen ? Color.LimeGreen: Color.Gray;

            db.UpdataConfigByName(
                new portconfig()
                {
                    COM = comboBox1.Text,
                    Baudrate = int.Parse(this.comboBox2.Text),
                    DataBits = int.Parse(this.comboBox3.Text),
                    Name = "LCR",
                    Parity = comboBox5.Text,
                    Stopbits = comboBox4.Text
                });


            config.SetValue("DAY", dateTimePicker1.Value.ToString("HH:mm") + "," + dateTimePicker2.Value.ToString("HH:mm"));
            config.SetValue("LINE", this.textBox2.Text);

           config.SetValue("SAVE_PATH", this.textBox1.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog openFileDialog = new FolderBrowserDialog();
            var result = openFileDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.textBox1.Text = openFileDialog.SelectedPath;
            }
        }
        System.Timers.Timer tmr = new System.Timers.Timer(10);
        private void button2_Click(object sender, EventArgs e)
        {
            var read = _io.Read();
    //        this.label8.Text = Convert.ToString(read, 2);
            _io.Pulse(1, 1000);
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

        private void usrSet_Load(object sender, EventArgs e)
        {
            //tmr.Start();
            //tmr.Elapsed += tmr_Elapsed;
            //tmr.Interval = 1;

        }

        void tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
      //      int v = IOC0640.ioc_read_inport(0, 0);
      ////      this.button1.Text = Convert.ToString(v, 2);
      //      if (InvokeRequired)
      //      {
      //          try
      //          {
      //              this.Invoke(new Action(() => button4.Text = Convert.ToString(v, 2)));
      //          }
      //          catch (Exception)
      //          {
                    
      //         //     throw;
      //          }
      //      }          
        }

        void update( int v)
        {
       //     textBox1.Text =Convert.ToString( v,2);
        }
        bool v;
        private void button4_Click(object sender, EventArgs e)
        {
       //     v = !v;
      //      var ret = IOC0640.ioc_write_outbit(0, 1, v ? 1 : 0);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            config.SetValue("ENABLE_COMM", this.checkBox1.Checked.ToString());
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            config.SetValue("ENABLE_RESULT", this.checkBox2.Checked.ToString());
        }
    }
}
